/* eslint valid-jsdoc: "off" */

'use strict';
const path = require('path');
const fs = require('fs');
/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
    /**
     * built-in config
     * @type {Egg.EggAppConfig}
     **/
    const config = (exports = {});
    // 配置图标
    // config.siteFile = {
    //     '/favicon.ico': fs.readFileSync(path.join(__dirname, 'favicon.ico')),
    // };

    // use for cookie sign key, should change to your own and keep security
    config.keys = appInfo.name + '_1610440745201_2663';

    // add your middleware config here
    config.middleware = [];

    // close csrf
    config.security = {
        csrf: {
            enable: false,
        },
    };
    // cors config
    config.cors = {
        origin: '*',
        allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS',
    };

    config.jwt = {
        secret: 'cnki',
    };

    // add your user config here
    const userConfig = {
        // myAppName: 'egg',
    };

    // sequelize config
    config.sequelize = {
        dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
        host: '127.0.0.1',
        port: 3306,
        database: 'db_template',
        username: 'root',
        password: '123456',
        timezone: '+08:00',
        define: { // model的全局配置
            timestamps: true, // 添加create,update,delete时间戳
            createdAt: 'created_at',
            updatedAt: 'updated_at',
            // paranoid: true, // 添加软删除
            freezeTableName: true, // 防止修改表名为复数
            underscored: false, // 防止驼峰式字段被默认转为下划线
        },
        dialectOptions: {
            // 让读取date类型数据时返回字符串而不是UTC时间
            dateStrings: true,
            typeCast(field, next) {
                if (field.type === 'DATETIME') {
                    return field.string();
                }
                return next();
            },
        },
    };
    // ejs config
    config.view = {
        mapping: {
            '.html': 'ejs',
        },
    };
    config.static = {
        prefix: '/',
        dir: [path.join(appInfo.baseDir, 'app/public'), path.join(appInfo.baseDir, 'app/dist')], // `String` or `Array:[dir1, dir2, ...]` 静态化目录,可以设置多个静态化目录
        dynamic: true, // 如果当前访问的静态资源没有缓存，则缓存静态文件，和`preload`配合使用；
        preload: false,
        maxAge: 31536000,
        buffer: true,
    };

    config.multipart = {
        mode: 'stream',
        fileExtensions: ['.doc', '.docx', '.pdf', '.caj', '.jpg', '.png', '.gif', '*'], // 增加对 apk 扩展名的文件支持
    };

    config.cluster = {
        // listen: {
        //     path: '',
        //     port: 3000,
        //     hostname: '0.0.0.0',
        // },
    };
    return {
        ...config,
        ...userConfig,
    };
};
